package com.example.programme.database;


import static com.example.programme.database.DBHelper.TB_NAME1;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.programme.Items.WordItem;

public class DBManager {

    private DBHelper dbHelper;
    private String TBNAME;

    public DBManager(Context context) {
        dbHelper = new DBHelper(context);
        TBNAME = DBHelper.TB_NAME1;
    }
    //添加
    public void add(String english,String cixing,String chinese ){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("English", english);
        values.put("Cixing", cixing);
        values.put("Chinese", chinese);
        db.insert(TB_NAME1, null, values);
        db.close();
    }

    public void delete(String str){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME, "English=?",new String[]{str});
        db.close();
    }

      public void update(String english,String cixing,String chinese){
          SQLiteDatabase db = dbHelper.getWritableDatabase();
          ContentValues values = new ContentValues();
          values.put("Cixing", cixing);
          values.put("Chinese", chinese);
          db.update(TB_NAME1, values, "English=?", new String[]{english});
          db.close();
    }

    @SuppressLint("Range")
    public List<WordItem> listAll(){
        List<WordItem> wordList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        if(cursor!=null){
            wordList = new ArrayList<WordItem>();
            while(cursor.moveToNext()){
                WordItem item = new WordItem();
                item.setId(cursor.getInt(cursor.getColumnIndex("ID")));
                item.setEnglish(cursor.getString(cursor.getColumnIndex("English")));
                item.setCixing(cursor.getString(cursor.getColumnIndex("Cixing")));
                item.setChinese(cursor.getString(cursor.getColumnIndex("Chinese")));

                wordList.add(item);
            }
            cursor.close();
        }
        db.close();
        return wordList;

    }
    //倒序
    @SuppressLint("Range")
    public List<WordItem> dxlistALL(){
        List<WordItem> wordList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        cursor.moveToLast();
        if(cursor.getCount()>0){
            wordList = new ArrayList<WordItem>();
            do{
                WordItem item = new WordItem();
                item.setId(cursor.getInt(cursor.getColumnIndex("ID")));
                item.setEnglish(cursor.getString(cursor.getColumnIndex("English")));
                item.setCixing(cursor.getString(cursor.getColumnIndex("Cixing")));
                item.setChinese(cursor.getString(cursor.getColumnIndex("Chinese")));

                wordList.add(item);
            } while(cursor.moveToPrevious());
            cursor.close();
        }
        db.close();
        return wordList;

    }
}
